package io.renren.modules.business.service.impl;

import io.renren.modules.business.dao.BusAssessmentScoreDao;
import io.renren.modules.business.entity.BusAssessmentScoreEntity;
import io.renren.modules.business.service.BusAssessmentScoreService;
import io.renren.modules.business.util.PageLastUtil;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;

@Service("busAssessmentScoreService")
public class BusAssessmentScoreServiceImpl extends ServiceImpl<BusAssessmentScoreDao, BusAssessmentScoreEntity> implements BusAssessmentScoreService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String userName = (String) params.get("userName");
        List<Map<String, Object>> list;
        if (!"".equals(userName) && userName.length()!=0) {
            //关键字查询
            list = baseMapper.developerInfo(userName);
        }else {
            //查询列表
            list = baseMapper.developerList();
        }
        //添加团队参与项目信息
        list = teamProjectList(list);
        return PageLastUtil.getPageUtils(params, list);
    }

    /**
     * 开发者绩效考核
     * @param score
     * @return
     */
    @Override
    public boolean scoreDeveloper(Long id, Double score) {
        Double proAverage = baseMapper.projectAverage(id);
        //判断开发者是否为队长
        int addPoint = (baseMapper.teamLeader(id) == 1) ? 2 : 0;
        //开发者绩效考核值
        Double developerScore = score * 0.70 + proAverage * 0.30 + addPoint;
        Long recordId = baseMapper.seleRecord(id);
        //将开发者分数、项目平均分存入打分表，添加学生考核记录
        baseMapper.developerScore(recordId, score, proAverage, 0.0);
        //将开发者绩效值存入绩效考核记录中
        baseMapper.developerRecord(id, developerScore);
        return true;
    }

    /**
     * 封装用户参与项目信息
     * @param list
     * @return
     */
    public List<Map<String, Object>> teamProjectList(List<Map<String, Object>> list){
        for (int i = 0; i<list.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            Long teamId = (Long) list.get(i).get("team_id");
            //团队参与项目信息
            String project = teamProject(teamId);
            if (project != null && project.length() !=0) {
                list.get(i).put("join_project", project);
            }else {
                list.get(i).put("join_project", " ");
            }
        }
        return list;
    }

    /**
     * 查询团队参与项目
     * @param teamId
     * @return
     */
    public String teamProject(Long teamId) {
        List<String> project = baseMapper.teamProject(teamId);
        String teamProject = " ";
        if (project.size() != 0) {
            for (String p : project) {
                teamProject =  p + "  ";
            }
        }
        return teamProject;
    }

}